/*
* @Author: Flora
* @Date:   2019-01-14 20:07:54
* @Last Modified by:   Flora
* @Last Modified time: 2019-01-21 13:49:20
*/
'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/footer/index.css');
var _mm = require('util/mm.js');

var allProductTemplate = require('./all-product.string');
var restProductTemplate = require('./rest-product.string');
var _product = require('service/product-service.js');
var _user = require('service/user-service.js');

var page = {
	allProducts : {},
	restProducts : [],
	init : function(){
		var _this = this;
		_this.bindEvent();			
		var type = _mm.getUrlParam('type');
		_user.getUser(function(resData){
			var user = resData;
			if(user == null || user.role == 0){
				// 用户未登录，或者buyer登录
				$('#rest-product').hide();
				if(user == null){
					_this.getAllProducts({
						saleTag : false,
						deleTag : false
					});					
				}else{
					// seller需要显示deleTag
					_this.getAllProducts({
						saleTag : false,
						deleTag : true
					});	
				}
			}else if(user.role == 1){
				// seller登录	
				if(type == 1){
					// 显示可购买内容
					_this.getRestProducts();
					$('#rest-product').addClass('z-sel');
					$('#all-product').removeClass('z-sel');
				}else{
					// 显示所有内容
					_this.getAllProducts({
						saleTag : true,
						deleTag : false}
						);
					$('#all-product').addClass('z-sel');
					$('#rest-product').removeClass('z-sel');
				}
				$('#rest-product').show();
			}
		});
	},
	bindEvent : function(){
		var _this = this;
		// 删除商品
		$('.n-plist #plist').delegate(".del", "click", function(event){
			var productId = $(this).data('del');
			event.stopPropagation();
			_this.createOrderTips(productId);
		});
		// 链接到商品详情
		$('.n-plist #plist').delegate(".link", "click", function(){
			var productId = $(this).data('link');
			window.location.href="./show.html?id="+productId;
		});
	},
	// 删除商品提示框
	createOrderTips : function(productId){
		var _this = this;
		// 显示提示框
		$('.m-index .m-winwrapper').css('display','table');
		// 监听确定按钮
		$('.m-index .btn-mini-y').click(function(){
			$('.m-index .m-winwrapper').css('display','none');
			_product.delete(productId);
			_this.init();
		});
		// 监听取消按钮
		$('.m-index .btn-mini-n').click(function(){
			$('.m-index .m-winwrapper').css('display','none');
		});
	},
	// 展示所有商品
	renderAll :function(){
		var _this = this;
		_this.render(allProductTemplate,{
			productList : _this.allProducts
		})
	},
	// 展示有库存的商品
	renderRest : function(){
		var _this = this;
		_this.render(restProductTemplate,{
			productList : _this.restProducts
		})
	},
	render : function(template, data){
		var productHtml = _mm.renderHtml(template, data);
		$('.f-cb').html(productHtml);
	},
	getAllProducts : function(tags){
		var _this = this;
		_product.getAllProducts(function(resData){
			_this.allProducts = resData;
			if(tags.saleTag){
				for(var i =0 ; i < resData.length ; i++){
					if(resData[i].status == 0){
						_this.allProducts[i]['noStock'] = true ;/*//售空*/
					}else{
						_this.allProducts[i]['noStock'] = false ;/*//有库存*/
					}
				}
			}
			if(tags.deleTag){
				for(var i=0 ; i < resData.length ; i++){
					if(resData[i].status == 0){
						_this.allProducts[i]['delBtn'] = false;
					}else{
						_this.allProducts[i]['delBtn'] =true;
					}
				}
			}
			_this.renderAll();
		}, function(errMsg){
			alert('获取失败，请重试');
		});
	},
	getRestProducts : function(){
		var _this = this;
		_product.getCanBuyProducts(function(resData){
			_this.restProducts = resData;
			_this.renderRest();
		}, function(errMsg){
			alert('获取失败，请重试');
		});
	}
};
$(function(){
	page.init();
});

